var searchType=-1;
jQuery("#grid").jqGrid({        
   	url:'trojan.php',
	datatype: "json",
	height: 200,
   	colNames:['记录编号','发现日期','发现时间','木马名称','源IP','目的IP','发送者','接受者','目的URL','协议类型','控制策略','通知情况','源端口','目的端口','详细信息','主题','内容','附件'],
   	colModel:[
   		{name:'ID',index:'ID', width:10,hidden:true},
   		{name:'DATE',index:'DATE', width:100},
   		{name:'TIME',index:'TIME', width:100},
   		{name:'NAME',index:'NAME', width:100},
   		{name:'SIP',index:'SIP', width:100},
   		{name:'DIP',index:'DIP', width:100},
   		{name:'SENDER',index:'SENDER', width:100},
   		{name:'RECEIVER',index:'RECEIVER', width:100},
	   {name:'URL',index:'URL', width:70},
	   {name:'PROTOCOL',index:'DEAL', width:60},
	   {name:'DEAL',index:'PROTOCOL', width:100,hidden:true},
	   {name:'ALARM',index:'ALARM', width:80},
	   {name:'SPORT',index:'SPORT', width:70},
   		{name:'DPORT',index:'DPORT', width:70},
	   {name:'DETAIL',index:'DETAIL', width:100,hidden:true},
	   {name:'SUBJECT',index:'SUBJECT', width:10,hidden:true},
	   {name:'CONTENT',index:'CONTENT', width:10,hidden:true},
	   {name:'ATTACHMENT',index:'ATTACHMENT', width:10,hidden:true},
   	],
    width:"100%",
    rowNum:20,
    rowList:[10,20,30],
   	 mtype: "POST",
   	 pager: jQuery('#pgrid'),
   	 sortname: 'ID',
    viewrecords: true,
    sortorder: "desc",
	 viewrecords: true,
    grouping:true, 
    gridComplete: function(){
		var ids = jQuery("#grid").jqGrid('getDataIDs');
		for(var i=0;i < ids.length;i++){
			var cl = ids[i];
			be = "<input style='height:22px;width:100px;' type='button' onclick=\"getMailDetail(+"+cl+")\" value='文件详细信息'/>"; 
			jQuery("#grid").jqGrid('setRowData',ids[i],{DETAIL:be});
		}	
	},
	onSelectRow: function(ids) { 
	var id = jQuery("#grid").jqGrid('getGridParam','selrow'); 
	if (id) { 
	var ret = jQuery("#grid").jqGrid('getRowData',id);
	} 
	else { 
	alert("请选择一行");
	}
	if(ids == null) { 
	ids=0; 
	if(jQuery("#gridip").jqGrid('getGridParam','records') >0 ) { 
	jQuery("#gridip").jqGrid('setGridParam',{url:"trojan.php?q=2&id="+ids,page:1}); 
	jQuery("#gridip").jqGrid('setCaption',"详细: "+ids) .trigger('reloadGrid'); 
	} 
	} 
	else { 
	 ids=ret.DIP;
	 var nm=ret.NAME;
	 if(searchType==0)
    {
   
    $( "#from1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	 $( "#to1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
    var from_mask = jQuery("#from1").val();
	 var to_mask = jQuery("#to1").val();
    }
    else if(searchType==1)
    {
    
    $( "#from2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	 $( "#to2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
    var from_mask = jQuery("#from2").val();
	 var to_mask = jQuery("#to2").val();

    }

	 var style=1;
	 jQuery("#gridip").jqGrid('setGridParam',{url:"trojan.php?q=2&nm_mask="+nm+"&from_mask="+from_mask+"&to_mask="+to_mask+"&style="+style,page:1}); 
	jQuery("#gridip").jqGrid('setCaption',"详细: "+ids) .trigger('reloadGrid'); 
	} 
	}
});
jQuery("#grid").jqGrid('navGrid','#pgrid',{del:false,add:false,edit:false},{height:150,reloadAfterSubmit:false},{height:150,reloadAfterSubmit:false},{reloadAfterSubmit:false},{multipleSearch:true}); 



jQuery("#gridip").jqGrid({        
	datatype: "json",
	height: 200,
   	colNames:['记录编号','木马名称','源端口','目的端口','非法IP','URL','发现日期 ','发现次数','控制类型','时段详细信息'],
   	colModel:[
	    //{name:'PIC',index:'PIC', width:200,sortable:false},
   		{name:'ID',index:'ID', width:100,hidden:true},
   		{name:'NAME',index:'NAME', width:150},
   		{name:'SPORT',index:'SPORT', width:75},
   		{name:'DPORT',index:'DPORT', width:75},
   		{name:'IP',index:'IP', width:150},
   		{name:'URL',index:'URL', width:150},
   		{name:'DATE',index:'DATE', width:100},
   		{name:'NUM',index:'NUM', width:100},
   		{name:'TYPE',index:'TYPE', width:100},
   		{name:'DETAIL',index:'DETAIL', width:150}

   	],
    width:"100%",
   	 rowNum:20,
    rowList:[10,20,30],
   	 mtype: "POST",
   	 pager: jQuery('#pgridip'),
   	 sortname: 'ID',
    viewrecords: true,
    sortorder: "desc",
	 viewrecords: true,
	gridComplete: function(){
		var ids = jQuery("#gridip").jqGrid('getDataIDs');
		for(var i=0;i < ids.length;i++){
			var cl = ids[i];
			a = "<input style='height:22px;width:100px;' type='button' onclick=\"getAccessDetail(+"+cl+")\" value='当天访问详细'/>";
			h = "<input style='height:22px;width:100px;' type='button' onclick=\"getHistoryDetail(+"+cl+")\" value='历史访问详细'/>"; 
			jQuery("#gridip").jqGrid('setRowData',ids[i],{DETAIL:a+h});
		}	
	},
	 onSelectRow: function(ids) { 
//	var id = jQuery("#gridip").jqGrid('getGridParam','selrow'); if (id) { var ret = jQuery("#gridip").jqGrid('getRowData',id);} else { alert("Please select row");}
//	if(ids == null) { 
//	ids=0; 
//	if(jQuery("#grid").jqGrid('getGridParam','records') >0 ) { 
//	jQuery("#grid").jqGrid('setGridParam',{url:"trojan.php?q=1&id="+ids,page:1}); 
//	jQuery("#grid").jqGrid('setCaption',"详细: "+ids) .trigger('reloadGrid'); 
//	} 
//	} 
//	else {
//	ids=ret.DIP;
//	 var sip=ret.SIP;
//	 if(searchType==0)
//    {
//   
//    $( "#from1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
//	 $( "#to1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
//    var from_mask = jQuery("#from1").val();
//	 var to_mask = jQuery("#to1").val();
//    }
//    else if(searchType==1)
//    {
//    
//    $( "#from2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
//	 $( "#to2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
//    var from_mask = jQuery("#from2").val();
//	 var to_mask = jQuery("#to2").val();

//    }

//	 var style=0;
//	 jQuery("#grid").jqGrid('setGridParam',{url:"trojan.php?q=2&sip_mask="+sip+"&from_mask="+from_mask+"&to_mask="+to_mask+"&style="+style,page:1}); 
//	jQuery("#grid").jqGrid('setCaption',"详细: "+ids) .trigger('reloadGrid');  
//	}
	}

});
jQuery("#gridip").jqGrid('navGrid','#pgridip',{del:false,add:false,edit:false},{height:150,reloadAfterSubmit:false},{height:150,reloadAfterSubmit:false},{reloadAfterSubmit:false},{multipleSearch:true}); 


//functions 
function complexSearch()
{
   //$("#s").dialog();
   searchType=1;
	$( "#c" ).dialog( { 
	       width:350,  
          height:250, 
	       buttons: { 
	      "取消": function() { $(this).dialog("close"); },
	      "提交":function(){

	           var style=0
	           var nm_mask = jQuery("#name2").val();
	           var sip_mask = jQuery("#sip").val();
	           var dip_mask = jQuery("#dip").val();
	           var send_mask = jQuery("#send").val();
	           var rev_mask=jQuery("#receiver").val();
	            $( "#from2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	            $( "#to2" ).datepicker( "option", "dateFormat", "yy-mm-dd");
              var from_mask = jQuery("#from2").val();
	           var to_mask = jQuery("#to2").val();
	           var deal_mask = jQuery("#deal2").val();
	           var url_mask=jQuery("#durl").val();
	           var pro_mask=jQuery("#protocol").val();
	           jQuery("#grid").jqGrid('setGridParam',{url:"trojan.php?q=1&from_mask="+from_mask+"&to_mask="+to_mask+"&sip_mask="+sip_mask+"&dip_mask="+dip_mask+"&send_mask="+send_mask+"&rev_mask="+rev_mask+"&nm_mask="+nm_mask+"&deal_mask="+deal_mask+"&url_mask="+url_mask+"&pro_mask="+pro_mask+"&style="+style,page:1}); 
	            jQuery("#grid").jqGrid('setCaption',"详细: ") .trigger('reloadGrid'); 
	         }
	         },
	       close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			} });
}
function simpleSearch()
{
   //$("#s").dialog();
   searchType=0;
	$( "#s" ).dialog( { 
	       width:350,  
          height:350, 
	       buttons: { 
	        "取消": function() { $(this).dialog("close"); },
	        "提交":function(){
	         
	            var style=0
	            $( "#from1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	            $( "#to1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
              var from_mask = jQuery("#from1").val();
	           var to_mask = jQuery("#to1").val();
	           var nm_mask = jQuery("#name1").val();
	            var deal_mask = jQuery("#deal1").val();
	           jQuery("#grid").jqGrid('setGridParam',{url:"trojan.php?q=1&from_mask="+from_mask+"&to_mask="+to_mask+"&nm_mask="+nm_mask+"&deal_mask="+deal_mask+"&style="+style,page:1}); 
	            jQuery("#grid").jqGrid('setCaption',"详细: ") .trigger('reloadGrid');  
	         }
	         },
	       close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			} });
}

function getMailDetail(cl)
{
   var ret = jQuery("#grid").jqGrid('getRowData',cl);
   var subject=ret.SUBJECT;
   var content=ret.CONTENT;
   var attachment=ret.ATTACHMENT;
   jQuery("#subject").attr("value",subject);
   jQuery("#content").attr("value",content);
   jQuery("#attachment").attr("value",attachment);
  $( "#content1" ).dialog({
	 	title: '文件详细信息',
	  width:200,  
    height:300,  
    modal:true
		});
   
}


function getAccessDetail(cl)
{
   var ret = jQuery("#gridip").jqGrid('getRowData',cl);
   var nm=ret.NAME;
   var date=ret.DATE;
   var type=ret.TYPE;
   var style=2;
   var number = Math.random();
 	 $( "#container" ).dialog({
	 	title: '访问详情',
	  width:800,  
    height:600,  
    modal:true
		});
			
// create the chart

jQuery.getJSON('trojan.php?number='+number+"&style="+style+"&from_mask="+date+"&nm_mask="+nm+"&deal_mask="+type, null, function(data) {
        	
        	var chart = new Highcharts.StockChart({
			chart : {
				renderTo : 'container'
			},

			title: {
				text: ' by minute'
			},
			
			rangeSelector : {
				buttons : [{
					type : 'second',
					count : 1,
					text : '1s'
				}],
				selected : 1,
				inputEnabled : false
			},
			
			series : [{
				name : '访问次数',
				data : data,
				tooltip: {
					valueDecimals: 2
				}
			}]
		});           
		
						});
   
}

function getHistoryDetail(cl)
{
   var ret = jQuery("#gridip").jqGrid('getRowData',cl);
   var nm=ret.NAME;
   var date=ret.DATE;
   var type=ret.TYPE;
   var style=3;
   var number = Math.random();
   	$( "#from" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	$( "#to" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	var from_mask = jQuery("#from").val();
	var to_mask = jQuery("#to").val();
	var strs= new Array();
	strs=date.split("-");
	var year=parseInt(strs[0],10);
	var month=parseInt(strs[1],10)-1;
	var day=parseInt(strs[2],10);

 	 $( "#container" ).dialog({
	 	title: '访问详情',
	  width:800,  
    height:600,  
    modal:true
		});
			
// create the chart

jQuery.getJSON('trojan.php?number='+number+"&style="+style+"&from_mask="+date+"&nm_mask="+nm+"&deal_mask="+type, null, function(data) {
        	
        	var chart = new Highcharts.StockChart({
			chart : {
				renderTo : 'container'
			},

			title: {
				text: ' by day'
			},
			
			rangeSelector : {
				buttons : [{
					type : 'day',
					count : 1,
					text : '1D'
				}],
				selected : 1,
				inputEnabled : false
			},
			
			series : [{
				name : '访问次数',
				data : data,
				tooltip: {
					valueDecimals: 2
				}
			}]
		});           
		
						});
   
}




